<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible.sync="showDialog" @closed="closeD">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form :model="formData" label-width="120px">
      <el-form-item label="部门名称">
        <el-input style="width: 80%" placeholder="1-50个字符" v-model="formData.name" />
      </el-form-item>
      <el-form-item label="部门编码">
        <el-input style="width: 80%" placeholder="1-50个字符" v-model="formData.code" />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select
          @focus="getEmployeeSimple"
          style="width: 80%"
          placeholder="请选择"
          v-model="formData.manager"
        >
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍">
        <el-input
          style="width: 80%"
          v-model="formData.introduce"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees';
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments';
export default {
  name: 'AddDept',
  data() {
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
      },
      peoples: [],
    };
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    treeNode: {
      type: Object,
    },
    typeStatus: {
      type: String,
    },
  },
  mounted() {
    // console.log(this.typeStatus);
    // if (this.typeStatus === 'edit') {
    //   console.log('0000000');
    //   this.formData = this.treeNode;
    // }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门';
    },
  },
  methods: {
    getSeValue(value) {
      console.log(value);
    },
    async getEmployeeSimple() {
      const res = await getEmployeeSimple();
      this.peoples = res.data.data;
    },
    async getDepartDetail(id) {
      const res = await getDepartDetail(id);
      this.formData = res.data.data;
    },
    async btnOk() {
      if (this.formData.id) {
        const res = await updateDepartments(this.formData);
        this.cancleDialog();
        this.$emit('delDepts');
      } else {
        console.log('新增');
        addDepartments({ ...this.formData, pid: this.treeNode.id }).then((res) => {
          this.cancleDialog();
          this.$emit('delDepts');
        });
      }
    },
    cancleDialog() {
      this.$emit('closeDia');
    },
    closeD() {
      this.formData = {};
    },
  },
};
</script>

<style lang="scss"></style>
